import { nextTick } from 'vue'
import * as svg from '@element-plus/icons-vue'

export default {
  getElementPlusIconfont() {
    return new Promise((resolve, reject) => {
      nextTick(() => {
        const icons = svg as any
        const sheetsIconList = []
        for (const i in icons) {
          sheetsIconList.push(`${icons[i].name}`)
        }
        if (sheetsIconList.length > 0) resolve(sheetsIconList)
        else reject('未获取到值，请刷新重试')
      })
    })
  },

  getAwesomeIconfont() {
    return new Promise((resolve, reject) => {
      nextTick(() => {
        const styles: any = document.styleSheets
        const sheetsList = []
        const sheetsIconList = []
        for (let i = 0; i < styles.length; i++) {
          if (
            styles[i].href &&
            styles[i].href.indexOf('cdnjs.cloudflare.com') > -1
          ) {
            sheetsList.push(styles[i])
          }
        }
        for (let i = 0; i < sheetsList.length; i++) {
          for (let j = 0; j < sheetsList[i].cssRules.length; j++) {
            if (
              sheetsList[i].cssRules[j].selectorText &&
              sheetsList[i].cssRules[j].selectorText.indexOf('.fa-') === 0 &&
              sheetsList[i].cssRules[j].selectorText.indexOf(',') === -1
            ) {
              if (/::before/.test(sheetsList[i].cssRules[j].selectorText)) {
                sheetsIconList.push(
                  `${sheetsList[i].cssRules[j].selectorText
                    .substring(1, sheetsList[i].cssRules[j].selectorText.length)
                    .replace(/\:\:before/gi, '')}`
                )
              }
            }
          }
        }
        if (sheetsIconList.length > 0) resolve(sheetsIconList.reverse())
        else reject('未获取到值，请刷新重试')
      })
    })
  },

  getAlicdnIconfont() {
    return new Promise((resolve, reject) => {
      nextTick(() => {
        const styles: any = document.styleSheets
        const sheetsList = []
        const sheetsIconList = []
        for (let i = 0; i < styles.length; i++) {
          if (styles[i].href && styles[i].href.indexOf('at.alicdn.com') > -1) {
            sheetsList.push(styles[i])
          }
        }
        for (let i = 0; i < sheetsList.length; i++) {
          for (let j = 0; j < sheetsList[i].cssRules.length; j++) {
            if (
              sheetsList[i].cssRules[j].selectorText &&
              sheetsList[i].cssRules[j].selectorText.indexOf('.icon-') > -1
            ) {
              sheetsIconList.push(
                `${sheetsList[i].cssRules[j].selectorText
                  .substring(1, sheetsList[i].cssRules[j].selectorText.length)
                  .replace(/\:\:before/gi, '')}`
              )
            }
          }
        }
        if (sheetsIconList.length > 0) resolve(sheetsIconList)
        else reject('未获取到值，请刷新重试')
      })
    })
  },

  getRemixIconfont() {
    return new Promise((resolve, reject) => {
      nextTick(() => {
        const styles: any = document.styleSheets
        const sheetsList = []
        const sheetsIconList = []
        for (let i = 0; i < styles.length; i++) {
          if (
            styles[i].href &&
            styles[i].href.indexOf('cdn.jsdelivr.net') > -1
          ) {
            sheetsList.push(styles[i])
          }
        }
        for (let i = 0; i < sheetsList.length; i++) {
          for (let j = 0; j < sheetsList[i].cssRules.length; j++) {
            if (
              sheetsList[i].cssRules[j].selectorText &&
              sheetsList[i].cssRules[j].selectorText.indexOf('.ri-') === 0 &&
              sheetsList[i].cssRules[j].selectorText.indexOf(',') === -1
            ) {
              if (/::before/.test(sheetsList[i].cssRules[j].selectorText)) {
                sheetsIconList.push(
                  `${sheetsList[i].cssRules[j].selectorText
                    .substring(1, sheetsList[i].cssRules[j].selectorText.length)
                    .replace(/\:\:before/gi, '')}`
                )
              }
            }
          }
        }
        if (sheetsIconList.length > 0) resolve(sheetsIconList.reverse())
        else reject('未获取到值，请刷新重试')
      })
    })
  }
}
